package com.education.teacher.controller;

import java.io.File;
import java.util.*;

import javax.servlet.http.HttpSession;

import com.education.model.StudyResModel;
import com.education.util.PropertiesUtil;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import com.alibaba.dubbo.config.annotation.Reference;
import com.education.model.ResourceTCModel;
import com.education.model.StudyResourceModel;
import com.education.service.IStudyResService;
import com.education.model.ResultDo;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.multipart.MultipartFile;

/**
 * 导学资源的控制层
 *
 * @author 申忠正
 */
@Api(value = "/api/teacher", description = "导学资源的相关操作")
@RestController
@RequestMapping("/api/teacher")
public class StudyResController {
    /**
     * 注入导学资源的业务层
     */
    @Reference
    private IStudyResService iStudyResService;
    /**
     * 引入log4j日志记录
     */
    private static Logger logger = LogManager.getLogger(StudyResController.class);


    /**
     * 查询所有当前登陆教师发布的资源
     * 2017/8/10 重写 by 雷紫辉
     *
     * @param pageSize
     * @param pageNum
     * @param session
     * @return
     * @throws Exception
     */
    @ApiOperation(notes = "StudyResController/queryResource", httpMethod = "GET", value = "查询所有的导学资源信息")
    @RequestMapping(value = "/StudyResController/queryResource", method = RequestMethod.GET)
    public ResultDo<PageInfo<StudyResModel>> selectStudyResource(int pageSize, int pageNum, HttpSession session) throws Exception {

        int tid = Integer.parseInt((String) session.getAttribute("teacherId"));
        PageInfo<StudyResModel> studyResModelPageInfo = iStudyResService.getStudyResByTeacherId(tid, pageNum, pageSize);
        return new ResultDo<>(studyResModelPageInfo);
    }


    @ApiOperation(notes = "StudyResController/addResource", httpMethod = "POST", value = "增加导学资源信息")
    @RequestMapping("/StudyResController/addResource")
    public ResultDo<Void> addStudyResource(MultipartFile resourceFile, StudyResModel studyResModel, HttpSession session) throws Exception {

        int tid = Integer.parseInt((String) session.getAttribute("teacherId"));

        String path = UUID.randomUUID().toString();//获取uuid为实际存储在硬盘的文件名，基路径写在配置文件中
        studyResModel.setTeacherId(tid);
        studyResModel.setResourcePath(path);
        studyResModel.setResourceName(resourceFile.getOriginalFilename());
        studyResModel.setResourceSize(resourceFile.getSize() / 1024);
        studyResModel.setResourceAddTime(new Date());
        int res = iStudyResService.addStudyResource(studyResModel);
        ResultDo<Void> resultDo = new ResultDo<>();
        if (res != 0) {
            resourceFile.transferTo(new File(PropertiesUtil.doc + path));
        }
        else {
            resultDo.setResCode(101);
        }
        return resultDo;
    }

    /**
     * 删除一条资料，数据库实际时更新  存储的文件不删除
     *
     * @param resourceId
     * @return
     * @throws Exception
     */
    @ApiOperation(notes = "StudyResController/deleteResource", httpMethod = "GET", value = "删除导学资源信息")
    @RequestMapping("/StudyResController/deleteResource")
    public ResultDo<Void> deleteResource(int resourceId) throws Exception {
        int result = iStudyResService.deleteResource(resourceId);
        ResultDo<Void> res = new ResultDo<>();
        if (result == 0) {
            res.setResCode(101);
        }
        return res;
    }

    /**
     * 根据资源id 查询详情
     * 2017/8/12 修改返回实体对象 by 雷紫辉
     *
     * @return 资源对象
     * @throws Exception
     */
    @ApiOperation(notes = "StudyResController/queryResById", httpMethod = "GET", value = "根据资源id 查询导学资源信息")
    @RequestMapping("/StudyResController/queryResById")
    public ResultDo<StudyResModel> queryResById(int resourceId) throws Exception {
        StudyResModel studyResModel = iStudyResService.queryResById(resourceId);
        ResultDo<StudyResModel> res = new ResultDo<>();
        if (studyResModel != null) {
            res.setResData(studyResModel);
        }
        else {
            res.setResCode(100);
        }
        return res;
    }

    /**
     * 2017/8/13 重写方法 by 雷紫辉
     *
     * @param resourceFile
     * @param studyResModel
     * @return ResultDo
     * @throws Exception
     */
    @ApiOperation(notes = "StudyResController/updateResource", httpMethod = "POST", value = "更新导学资源信息")
    @RequestMapping(path = "/StudyResController/updateResource", method = RequestMethod.POST)
    public ResultDo<Void> updateResource(MultipartFile resourceFile, StudyResModel studyResModel) throws Exception {

        if (resourceFile != null) {
            studyResModel.setResourceName(resourceFile.getOriginalFilename());
            studyResModel.setResourceSize(resourceFile.getSize());
            studyResModel.setResourceAddTime(new Date());
        }

        int result = iStudyResService.updateResource(studyResModel);
        ResultDo<Void> res = new ResultDo<>();
        if (result > 0) {
            resourceFile.transferTo(new File(PropertiesUtil.doc + studyResModel.getResourcePath()));//覆盖原有文件
        }
        else {
            res.setResCode(101);
        }
        return res;
    }


}